package com.woniuxy.company5.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.company5.dto.MacApplyDto;
import com.woniuxy.company5.dto.MacApplyDto1;
import com.woniuxy.company5.entity.MacApply;
import com.woniuxy.company5.service.MacApplyService;
import com.woniuxy.company5.util.Result;
import com.woniuxy.company5.vo.MacApplyVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/macapply")
public class MacApplyController {
    @Resource
    private MacApplyService macApplyService;

    @GetMapping("{id}")
    public Result selOneMacApply(@PathVariable Long id){
       return Result.ok().data("oneMacApply",macApplyService.selOneById(id));
    }

    @PostMapping ("selAllMacApply")
    public Result selAllMacApply(@RequestBody MacApplyDto macApplyDto){
        PageHelper.startPage(macApplyDto.getCurrentPage(),macApplyDto.getPageSize(),true);
        PageHelper.orderBy("id desc");
        List<MacApplyVo> macApplies = macApplyService.selAllMacApply(macApplyDto);
        PageInfo<MacApplyVo> pageInfo = new PageInfo<>(macApplies);
        return Result.ok().data("macApplyList",macApplies).data("total",pageInfo.getTotal());
    }

    @PostMapping("")
    public Result addMacApply(@RequestBody MacApply macApply){
        macApply.setDate(new Date());
        macApply.setNum(new Date().getTime()+"");
        macApply.setEmpId(4);
        macApply.setState(1);
        macApplyService.addMacApply(macApply);
        return Result.ok();
    }

    @PutMapping("")
    public Result updateMacApply(@RequestBody MacApplyDto1 applyDto1){
        macApplyService.update(applyDto1);
        return Result.ok();
    }
}
